package com.example.arithmeticleetcode.leetcode.jan;

import java.util.Arrays;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2021-01-04 09:10
 **/
public class Demo01 {

    public int maximumUnits(int[][] boxTypes, int truckSize) {
        Arrays.sort(boxTypes, (o1, o2) -> o2[1] - o1[1]);
        int ret = 0;
        for (int[] boxType : boxTypes) {
            int temp = truckSize;
            truckSize -= boxType[0];
            if (truckSize > 0) {
                ret += boxType[0] * boxType[1];
            } else {
                ret += Math.abs(temp * boxType[1]);
                break;
            }
        }
        return ret;
    }

    /**
     * 斐波那契数列
     *
     * @param n
     * @return
     */
    public int fib(int n) {
        if (n < 1) return 0;
        if (n < 3) return 1;
        int pre = 1;
        int cur = 1;
        for (int i = 3; i <= n; i++) {
            int temp = cur;
            cur = pre + cur;
            pre = temp;
        }
        return cur;
    }

    public int fib2(int n) {
        if (n == 0) return 0;
        if (n == 1) return 1;
        return fib(n - 1) + fib(n - 2);
    }

    public int fib3(int n) {
        if (n == 0) return 0;
        if (n == 1) return 1;
        int[] arr = new int[n + 1];
        arr[0] = 0;
        arr[1] = 1;
        for (int i = 2; i <= n; i++) {
            arr[i] = arr[i - 1] + arr[i - 2];
        }
        return arr[n];
    }


}
